<?php
/**
 * 实时资金查询
 */
namespace app\data\controller;
use \think\Controller;
use \think\Db;

class Coin extends Controller
{
    public function index()
    {
        //查询系统内置用户表用于比对
        $sys_user=db('add_name')->field('account,id')->select();
        //url
        $url_user=LOGIN_HOST.'/trader/proxy/organize/account/aiaccount/query/page';
        $url_user_data = array (
            'search_LIKE_relationUserOrg.userSecurity.loginName' => '',
            'search_EQ_relationUserOrg.orgId' => '',
            'page'=>'1',
            'limit'=>'10000'
        );

        $content = GetPost($url_user, COOKIE_PATH,$url_user_data);
        //过滤采集数据
        $data=json_decode($content);

        //清空客户表准备重新写入信息
        if($data){
            $delete_sql="truncate table ke_coin";
            Db::execute($delete_sql);
        }else{
            sendMail(['6325610@qq.com'=>'可是','381508990@qq.com'=>'波'],'采集失败-实时资金查询',$content);
            return ;
        }
        //输出内容
        $res=$data->results->content;

        $arr=array();
        //批量写入数据库
        foreach ($res as $k => $it){

            if($it->exLogin){

                //交易账号
                $arr[$k]['account']=$it->exLogin;
                //用户名
                $arr[$k]['user']=$it->userName;
                //净值
                $arr[$k]['net_value']=$it->netWorth;
                //结余
                $arr[$k]['re_value']=$it->balance;
                //保证金
                $arr[$k]['bail_value']=$it->deposit;
                //可用保证金
                $arr[$k]['free_value']=$it->usableDeposit;
                //冻结
                $arr[$k]['freeze_value']=$it->freeze;
                //浮动盈亏
                $arr[$k]['yk_value']=$it->floatProfit;
                //风险
                $arr[$k]['risk']=$it->riskRate;

                foreach ($sys_user as $sys){
                    if($arr[$k]['account'] == $sys['account'] ){
                        $arr[$k]['pid'] = $sys['id'];
                        break;
                    }
                }


            }

        }

        db("coin")->insertAll($arr);
        sendMail(['381508990@qq.com'=>'波仔'],'实时资金查询表更新成功',$content);

    }

}
